<?php

require_once( '../../libs/smarty.inc' );

require_once( '../../libs/Class_DB.php' );

require_once( '../../libs/Class_ERROR.php' );

require_once( '../../libs/login.inc.php' );

require_once( '../../libs/const.php' );



if( !in_array( "A", $_SESSION['ADMIN_LOGIN']['AUTH_CD'] ) ) exit;



$db = new Class_DB;



// 製品・単価を読込み

$db->query( "SELECT * FROM M_PRODUCTS WHERE category = 5" );

$products[''] = '';

while( $row = $db->fetch() ){

	$products[ $row['id'] ] = $row['pro_name'];

	$costs[ $row['id'] ] = $row['cost'];

}





if( $_SERVER['REQUEST_METHOD'] == 'GET' ){

	

	if( isset($_GET['id']) && $_GET['id'] != '' ){

		

		if( is_numeric( $_GET['id'] ) ){

			

			$sql = sprintf("SELECT * FROM D_SHIPMENT WHERE id = %u", $_GET['id'] );

			$res = $db->query( $sql );

			$forms = mysql_fetch_array( $res, MYSQL_ASSOC );

			

			$sql = sprintf("SELECT * FROM D_SHIPMENT_DETAIL WHERE id = %u ORDER BY num", $_GET['id'] );

			$db->query( $sql );

			while( $row = $db->fetch() ){

				$forms['products'][$row['num']]	= $row['pro_cd'];

				$forms['denpyo'][$row['num']]	= $row['denpyo'];

				$forms['amount'][$row['num']]	= $row['amount'];

				$forms['from'][$row['num']]		= $row['sfrom'];

				$forms['biko'][$row['num']]		= $row['biko'];

				$forms['pro_name'][$row['num']]	= $row['pro_name'];

				$forms['cost'][$row['num']]		= $row['cost'];

				$forms['unit'][$row['num']]		= $row['unit'];

			}



		}



	}

	

}



$all_total = 0;

if( isset($forms['products']) ){

	foreach( $forms['products'] as $k => $v ){

		if( $forms['amount'][$k] != '' && is_numeric( $forms['amount'][$k] ) ){

			$all_total += $forms['cost'][ $k ] * $forms['amount'][$k];

			$subtotal[$k] = number_format( $forms['cost'][ $k ] * $forms['amount'][$k] );

		}

	}

}



//if( isset( $forms['id'] ) && $forms['id'] != '' ) $forms['disp_id'] = 'B' . sprintf( '%05d',$forms['id'] );

$forms['disp_id'] = 'B' . sprintf( '%05d',$forms['id'] );



$smarty->assign('products', $products);

$smarty->assign('costs', $costs);

$smarty->assign('specify', array( 1=> 'あり', 0=> 'なし'));

$smarty->assign('category', $category );

$smarty->assign('arr_denpyo', $arr_denpyo );

$smarty->assign('arr_result', $arr_result );

$smarty->assign('all_total', number_format($all_total) );



if( isset( $subtotal ) )$smarty->assign('subtotal', $subtotal);

if( isset( $forms ) )	$smarty->assign('forms', $forms);



$smarty->display('web-admin/ship_preview.html');





?>